//
//  ZDDLyricView.m
//  QQ音乐(皇家尊享版)
//
//  Created by 周冬冬 on 16/7/21.
//  Copyright © 2016年 我的mac. All rights reserved.
//

#import "ZDDLyricView.h"
#import <Masonry/Masonry.h>
#import "ZDDLynic.h"
#import "ZDDColorLabel.h"
static CGFloat const kCZLyricLabelHeight = 40;
#define ZDDLyricViewTop ((self.bounds.size.height - kCZLyricLabelHeight) * 0.5)

@interface ZDDLyricView()<UIScrollViewDelegate>
//横向滚动的视图
@property (nonatomic, strong) UIScrollView *hScrollView;
//竖向滚动视图
@property (nonatomic, strong) UIScrollView *vScrollView;

@end
@implementation ZDDLyricView

//从文件中创建时调用
-(instancetype)initWithCoder:(NSCoder *)aDecoder{

    self = [super initWithCoder:aDecoder];
    if (self) {
        
        [self prepareLyricView];
    }

    return self;
}
//设置歌词视图
-(void)prepareLyricView{

    //添加横向视图
    self.hScrollView = [[UIScrollView alloc] init];
    [self addSubview:self.hScrollView];
    //占满屏幕
    [self.hScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    //添加竖向视图
    
    self.vScrollView = [[UIScrollView alloc] init];
    [self.hScrollView addSubview:self.vScrollView];
    //约束
    [self.vScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, [UIScreen mainScreen].bounds.size.width, 0, 0));
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
        make.height.mas_equalTo(self);
    }];
    //设置横向视图
    self.hScrollView.showsHorizontalScrollIndicator = NO;
    self.hScrollView.bounces = NO;
    self.hScrollView.pagingEnabled = YES;
    //监听滚动
    self.hScrollView.delegate = self;


}
#pragma mark - setter&gett
-(void)setLyricModels:(NSArray *)lyricModels{

    //移除所有姿势图
    [self.vScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _lyricModels = lyricModels;
    for (int i = 0; i < _lyricModels.count; i++) {
        //取出歌词模型
        ZDDLynic * lyric = _lyricModels[i];
        //创建label
        ZDDColorLabel * label = [[ZDDColorLabel alloc] init];
        label.textColor = [UIColor whiteColor];
        label.text = lyric.content;
        [self.vScrollView addSubview:label];
        //设置位置
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.vScrollView);
            make.height.mas_equalTo(kCZLyricLabelHeight);
            //顶部
            make.top.mas_equalTo(kCZLyricLabelHeight * i);
            
        }];
    }


    //设置偏移量
    self.vScrollView.contentSize = CGSizeMake(0, _lyricModels.count * kCZLyricLabelHeight);

}

-(void)layoutSubviews{

    [super layoutSubviews];
    
    //设置内边据
    self.vScrollView.contentInset = UIEdgeInsetsMake(ZDDLyricViewTop, 0, ZDDLyricViewTop, 0);
    
    
    //设置默认的偏移量
    self.vScrollView.contentOffset = CGPointMake(0, -ZDDLyricViewTop);




}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView == self.hScrollView) {
        
    
    if ([self.delegate respondsToSelector:@selector(hScrollViewWithProgress:)]) {
        [self.delegate hScrollViewWithProgress:self.hScrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width];
    }

    }


}
-(void)setLyricIndex:(NSInteger)lyricIndex{


    //是否在切割
    if (lyricIndex != 0) {
        
        //提前将之前的索引对应的字号改回来
        ZDDColorLabel *lastLabel = self.vScrollView.subviews[_lyricIndex];
        lastLabel.font = [UIFont systemFontOfSize:17];
        lastLabel.progress = 0;

    }

    _lyricIndex = lyricIndex;
    //设置高度
    self.vScrollView.contentOffset = CGPointMake(0, -ZDDLyricViewTop + _lyricIndex * kCZLyricLabelHeight);
    //当前label字体变大
    UILabel * currentLabel = self.vScrollView.subviews[_lyricIndex];
    currentLabel.font = [UIFont systemFontOfSize:25];




}
-(void)setLyricProgress:(CGFloat)lyricProgress{

    _lyricProgress = lyricProgress;
    //设置当前label的进度
    ZDDColorLabel * label = self.vScrollView.subviews[_lyricIndex];
    

    label.progress = lyricProgress;



}
@end












